/* model */
import Field from '@model/entity/Field'
import { ConnectorBizTypeEnum, ConnectorModuleComponentNameEnum } from '@src/modules/connector/model'
/* mixin */
import { ConnectorModuleConnectorCreateMixin } from '@src/modules/connector/mixin'
/* vue */
import Component from 'vue-class-component'
/* util */
import Log from '@src/util/log'
import { initialize } from '@src/component/form/util'
import { packToForm } from '@src/modules/product/utils/ProductMapping'
import { getConnectorCreateDataSessionKeyByBizType } from '@src/modules/connector/util'

@Component({ 
  name: ConnectorModuleComponentNameEnum.ConnectorModuleConnectorCreateProductMixin,
})
export default class ConnectorModuleConnectorCreateProductMixin extends ConnectorModuleConnectorCreateMixin {
  
  /* 连接器新建数据 session 键 */
  public connectorCreateDataSessionKey = getConnectorCreateDataSessionKeyByBizType(ConnectorBizTypeEnum.Product)
  
  /* 连接下新建配置数据 */
  public connectorMessageData: Record<string, any> = {}
  
  /* 混入的组件需要重写此属性 */
  private form: Record<string, any> = {}
  
  /* 混入的组件需要重写此属性 */
  get productFields(): Field[] {
    return []
  }
  
  /** 
   * @description 连接器新建数据处理
  */
  public connectorInsertSelectCallDataHandler(data: Record<string, any>) {
    
    Log.info(data, 'data', this.connectorInsertSelectCallDataHandler.name)
    
    // 初始化解析表单数据
    let form = packToForm(this.productFields, data)
    
    Log.info(form, 'form', this.connectorInsertSelectCallDataHandler.name)
    
    const newValue = initialize(this.productFields, form)
    
    Object.keys(newValue).forEach(key => {
      const value = newValue[key]
      this.$set(this.form, key, value)
    })
    
    Log.info(this.form, 'this.form', this.connectorInsertSelectCallDataHandler.name)
    
  }
  
}

